package org.finmanager.core;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Kelvin on 2/4/2014.
 */

//Calling different models and UIs
public class CategoryManagerController implements ActionListener {
	public static final String OPEN_CATEGORY_INPUT = "OPEN_CATEGORY_INPUT";

	public static final String ADD_CATEGORY = "ADD_CATEGORY";
	public static final String DELETE_CATEGORY = "DELETE_CATEGORY";

	private final ItemManagerController itemManagerController;

	private CategoryModel categoryModel;
	private CategoryManagerUI categoryManagerUI;

	private CategoryInputUI inputUI;

	public CategoryManagerController(FinManager finManager, ItemManagerController itemManagerController) {
		this.itemManagerController = itemManagerController;

		categoryModel = finManager.getCategoryModel();
		categoryModel.getCategoryTableModel().addTableModelListener(itemManagerController.getFavouritesPanelUI());

		itemManagerController.getFavouritesPanelUI().setCategories(finManager.getCategoryList());
	}

	public ItemManagerController getItemManagerController() {
		return itemManagerController;
	}

	public CategoryModel getCategoryModel() {
		return categoryModel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String ac = e.getActionCommand();

		if (ac.equals(ItemManagerController.OPEN_CATEGORY_MANAGER))
		{
			categoryManagerUI = new CategoryManagerUI(CategoryManagerController.this);
			categoryManagerUI.buttonActionListener(this);
		}
		else if (ac.equals(OPEN_CATEGORY_INPUT))
		{
			inputUI = new CategoryInputUI(this);
			inputUI.addButtonListener(this);
		}
		else if (ac.equals(ADD_CATEGORY))
		{
			categoryModel.addCategory(new Category(inputUI.getNameTextField()));
		}
		else if (ac.equals(DELETE_CATEGORY))
		{
			int row = categoryManagerUI.getSelectedRow();
			categoryModel.deleteCategory(row);
		}
	}
}
